//
//  JDPaymentMethodView.swift
//  DragonWallet
//
//  Created by volantPig on 2025/2/2.
//

import UIKit

class JDPaymentMethodView: UIView {

    /// 支付宝付款回调
    var confirmButtonSelected: (()->Void)?
    /// 微信付款回调
    var wechatPayCallBack: (()->Void)?
    
    var cancelCallBack: (()->Void)?
    
    var containerView: UIView?
    var backButton: UIButton?
    var confirmButton: UIButton?
    
    var paymentTableView: UITableView?
    var selectedPaymentIndex: Int = -1
    
    var titleLabel: UILabel?
    var priceLabel: UILabel?
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, screenWidth, screenHeight))
        self.setupConfig()
        self.setupUIElements()
    }
    
    func setupConfig() {
        self.backgroundColor = color("#000000").withAlphaComponent(0.4)
    }
    
    func setupUIElements() {
        
        self.containerView = UIView()
        self.addSubview(self.containerView!)
        self.containerView?.snp.makeConstraints({ make in
            make.left.right.bottom.equalTo(self)
            
        })
        self.containerView?.backgroundColor = .white
        
        self.backButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.backButton!)
        self.backButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.containerView!).offset(12)
            make.right.equalTo(self.containerView!.snp.centerX).offset(-5)
            make.bottom.equalTo(self.containerView!).offset(-25)
            make.height.equalTo(54)
        })
        self.backButton?.layer.cornerRadius = 27
        self.backButton?.backgroundColor = color("#F5F6F8")
        self.backButton?.setTitle("取消", for: .normal)
        self.backButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.backButton?.setTitleColor(.black, for: .normal)
        self.backButton?.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        
        self.confirmButton = UIButton(type: .custom)
        self.containerView?.addSubview(self.confirmButton!)
        self.confirmButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.containerView!).offset(-12)
            make.left.equalTo(self.containerView!.snp.centerX).offset(5)
            make.bottom.equalTo(self.containerView!).offset(-25)
            make.height.equalTo(54)
        })
        self.confirmButton?.setTitle("确定支付", for: .normal)
        self.confirmButton?.layer.cornerRadius = 27
        self.confirmButton?.backgroundColor = color("#333333")
        self.confirmButton?.setTitleColor(.white, for: .normal)
        self.confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.confirmButton?.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        
        self.paymentTableView = UITableView(frame: .zero, style: .plain)
        self.containerView?.addSubview(self.paymentTableView!)
        self.paymentTableView?.snp.makeConstraints { make in
            make.left.equalTo(self.containerView!).offset(20)
            make.right.equalTo(self.containerView!).offset(-20)
            make.bottom.equalTo(self.confirmButton!.snp.top).offset(-15)
            make.height.equalTo(52*2)
        }
        self.paymentTableView?.delegate = self
        self.paymentTableView?.dataSource = self
        self.paymentTableView?.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
        self.paymentTableView?.separatorStyle = .none
        self.paymentTableView?.isScrollEnabled = false
        
        let line = UIView()
        self.containerView?.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(self.containerView!).offset(24)
            make.right.equalTo(self.containerView!).offset(-24)
            make.bottom.equalTo(self.paymentTableView!.snp.top).offset(-12)
            make.height.equalTo(1)
        }
        line.backgroundColor = color("#F5F6F8")
        
        self.priceLabel = UILabel()
        self.containerView?.addSubview(self.priceLabel!)
        self.priceLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.containerView!)
//            make.bottom.equalTo(self.confirmButton!.snp.top).offset(-75)
            make.bottom.equalTo(line.snp.top).offset(-17)
            make.height.equalTo(50)
        })
        self.priceLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 36, weight: .semibold), textColor: color("#333333"), numberOfLines: 1)
//        self.priceLabel?.text = "￥2388.00"
        self.priceLabel?.priceString(UIFont.systemFont(ofSize: 18, weight: .semibold), UIFont.systemFont(ofSize: 36, weight: .semibold))
        
        self.titleLabel = UILabel()
        self.containerView?.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ make in
            make.centerX.equalTo(self.containerView!)
            make.bottom.equalTo(self.priceLabel!.snp.top).offset(-20)
            make.top.equalTo(self.containerView!).offset(28)
            make.height.equalTo(22)
        })
        self.titleLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 16, weight: .semibold), textColor: color("#333333"))
        self.titleLabel?.text = " 支付方式"
        self.titleLabel?.appendImgV("alipay_image", at: 0)
        
        
        
        
        
        
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func cancelAction() {
        self.cancelCallBack?()
        self.removeFromSuperview()
    }
    
    @objc func confirmAction() {
        if self.selectedPaymentIndex < 0 {
            JDTools.getKeyWindows()?.rootViewController?.view.toast(message: "请选择支付方式")
            return
        }
        
        if selectedPaymentIndex == 0 {
            // 回调 拉起支付宝
            self.confirmButtonSelected?()
        } else {
            self.wechatPayCallBack?()
        }
        
    }
}

extension JDPaymentMethodView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "mycell")
        
        cell?.selectionStyle = .none
        

//        if indexPath.row%2 == 0 {
//            cell?.contentView.backgroundColor = .red
//        } else {
//            cell?.contentView.backgroundColor = .yellow
//        }
        
        for subV in cell?.contentView.subviews ?? [UIView]() {
            subV.removeFromSuperview()
        }
        
        let paymentTypeImageV = UIImageView()
        cell?.contentView.addSubview(paymentTypeImageV)
        paymentTypeImageV.snp.makeConstraints { make in
            make.left.equalTo(cell!.contentView)
            make.centerY.equalTo(cell!.contentView)
            make.size.equalTo(CGSize(width: 22, height: 22))
        }
        
        let paymentTypeLabel = UILabel()
        cell?.contentView.addSubview(paymentTypeLabel)
        paymentTypeLabel.snp.makeConstraints { make in
            make.left.equalTo(paymentTypeImageV.snp.right).offset(6)
            make.centerY.equalTo(paymentTypeImageV)
        }
        paymentTypeLabel.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 16, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        
        let rightImageV = UIImageView()
        cell?.contentView.addSubview(rightImageV)
        rightImageV.snp.makeConstraints { make in
            make.right.equalTo(cell!.contentView)
            make.centerY.equalTo(cell!.contentView)
            make.size.equalTo(CGSize(width: 22, height: 22))
        }
        
        if self.selectedPaymentIndex == indexPath.row {
            rightImageV.image = UIImage(named: "circle_selected")
        } else {
            rightImageV.image = UIImage(named: "circle_unSelected")
        }
        
//        selectedButton.setImage(UIImage(named: "circle_unSelected"), for: .normal)
//        selectedButton.setImage(UIImage(named: "circle_selected"), for: .selected)
        if indexPath.row == 0 {
            paymentTypeImageV.image = UIImage(named: "alipay_image")
            paymentTypeLabel.text = "支付宝"
        } else {
            paymentTypeImageV.image = UIImage(named: "wechat_image")
            paymentTypeLabel.text = "微信"
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.row == self.selectedPaymentIndex {
            self.selectedPaymentIndex = -1
        } else {
            self.selectedPaymentIndex = indexPath.row
        }
        tableView.reloadData()
    }
    
}
